package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public Page<Setmeal> getSetmealPage(String name, Integer pageNum, Integer pageSize) {
        //首先进行模糊条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);

        Page<Setmeal> page = new Page<>(pageNum,pageSize);
        page = setmealMapper.selectPage(page,wrapper);

        List<Setmeal> list = page.getRecords();
        if (CollectionUtil.isNotEmpty(list)) {
            for (Setmeal setmeal : list) {

                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());

                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishList);
            }
        }
        return page;
    }
    @Transactional
    @Override
    public void save(Setmeal setmeal) {
        //首先保存套餐数据
        setmealMapper.insert(setmeal);
        //随后保存套餐中的菜品数据
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        //判断后保存
        if (CollectionUtil.isNotEmpty(setmealDishes)){
            for (SetmealDish setmealDish : setmealDishes) {
                //设置每个套餐菜品的所属套餐id
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public Setmeal findById(Long id) {

        //首先获取setmeal数据
        Setmeal setmeal = setmealMapper.selectById(id);
        //在获取setmeal的dish
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);

        setmeal.setSetmealDishes(setmealDishes);

        return setmeal;
    }
    @Transactional
    @Override
    public void updateSetmeal(Setmeal setmeal) {
        //首先更新setmeal数据
        setmealMapper.updateById(setmeal);
        //再更新setmealdish数据，首先删除后新增
        //删除
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper);
        //再次添加
        //先获取更新后的dish数据
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                //设置每个套餐菜品的所属套餐id
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public void delete(Long[] ids) {
        //首先确定删除的套餐中有没有未停售的
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        Integer count = setmealMapper.selectCount(wrapper);
        if (count > 0){
            throw new CustomException("对不起你选择删除的套餐，还存在正在销售的无法删除");
        }

        //删除
        setmealMapper.deleteBatchIds(Arrays.asList(ids));

        //删除相关套餐的菜品

        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper1);


    }

    @Override
    public void status(Integer status, Long[] ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        if (CollectionUtil.isNotEmpty(Arrays.asList(ids))){
            for (Long id : ids) {
                Setmeal setmeal = new Setmeal();
                setmeal.setId(id);
                setmeal.setStatus(status);
                setmealMapper.updateById(setmeal);
            }
        }
    }

    @Override
    public List<Setmeal> getSetmeal(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId);
        wrapper.eq(Setmeal::getStatus,status);

        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);

        return setmealList;
    }

    @Override
    public List<SetmealDish> getSetmealDish(Long id) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        return setmealDishList;
    }
}
